﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web;
using System.Web.Mvc.Html;
using System.Web.Mvc.Ajax;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using System.IO;

namespace LifeTree.Mvc.Extensions
{
    public static class HtmlHelperExtensions
    {
        #region UrlActionLink

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url)
        {
            return htmlHelper.UrlActionLink(linkText, url, new RouteValueDictionary(), true, null);
        }

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url, string javascript)
        {
            return htmlHelper.UrlActionLink(linkText, url, new RouteValueDictionary(), true, javascript);
        }

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url, string javascript, bool encode)
        {
            return htmlHelper.UrlActionLink(linkText, url, new RouteValueDictionary(), encode, javascript);
        }

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url, bool encode)
        {
            return htmlHelper.UrlActionLink(linkText, url, new RouteValueDictionary(), encode, null);
        }

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url, object htmlAttributes)
        {
            return htmlHelper.UrlActionLink(linkText, url, new RouteValueDictionary(htmlAttributes), true, null);
        }

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url, object htmlAttributes, string javascript)
        {
            return htmlHelper.UrlActionLink(linkText, url, new RouteValueDictionary(htmlAttributes), true, javascript);
        }

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url, object htmlAttributes, string javascript, bool encode)
        {
            return htmlHelper.UrlActionLink(linkText, url, new RouteValueDictionary(htmlAttributes), encode, javascript);
        }

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url, object htmlAttributes, bool encode)
        {
            return htmlHelper.UrlActionLink(linkText, url, new RouteValueDictionary(htmlAttributes), encode, null);
        }

        public static IHtmlString UrlActionLink(this HtmlHelper htmlHelper, string linkText, string url, RouteValueDictionary htmlAttributes, bool encode, string javascript)
        {
            TagBuilder builder2 = new TagBuilder("a");
            builder2.InnerHtml = !string.IsNullOrEmpty(linkText) ? encode ? HttpUtility.HtmlEncode(linkText) : linkText : string.Empty;
            TagBuilder builder = builder2;
            builder.MergeAttributes<string, object>(htmlAttributes);

            if (!string.IsNullOrEmpty(javascript))
            {
                builder.MergeAttribute("onclick", javascript);
                builder.MergeAttribute("href", "javascript:void(0);");
            }
            else
            {
                builder.MergeAttribute("href", url);
            }
            return htmlHelper.Raw(builder.ToString(TagRenderMode.Normal));
        }

        #endregion

        #region Forms

        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string url)
        {
            return htmlHelper.BeginForm(url, null);
        }

        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string url, object htmlAttributes)
        {
            return htmlHelper.BeginForm(url, new RouteValueDictionary(htmlAttributes));
        }

        public static MvcForm BeginForm(this HtmlHelper htmlHelper, string url, IDictionary<string, object> htmlAttributes)
        {
            TagBuilder builder = new TagBuilder("form");
            builder.MergeAttributes<string, object>(htmlAttributes);
            builder.MergeAttribute("action", url);
            builder.MergeAttribute("method", HtmlHelper.GetFormMethodString(FormMethod.Post), true);
            htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
            MvcForm form = new MvcForm(htmlHelper.ViewContext);
            return form;
        }

        #endregion
    }
}
